# _*_ coding:utf-8 _*_
import json

from django.core.serializers import deserialize

from django import template
from movie.models import Movie,Review
from auser.models import User
register = template.Library()


# 都支持关键字参数形式
# 包含标签以模板文件为载体，载入，跟include差不多
@register.inclusion_tag('movie/_side_review.html')
def most_review(movie):
	context = {"reviews": None}
	reviews = movie.review_set.all().order_by('score')
	print(reviews)
	count = reviews.count()
	print(count)
	context['count'] = count
	context['movie'] = movie
	if not reviews:
		return context
	try:
		reviews = reviews[:10]
	except IndexError:
		context['reviews'] = reviews
	else:
		context['reviews'] = reviews
	return context


@register.assignment_tag
def fulltext_result(result):
	print(result)
	datas= []
	if result.get('movie'):
		movies=result.get('movie')
		for m in movies:
			datas.append(m)
	if result.get('review'):
		revs = result.get('review')
		for r in revs:
			datas.append(r)
	if result.get('user'):
		us=result.get('user')
		for u in us:
			datas.append(u)
	print(datas[:2])
	return datas


@register.filter
def splice(value,arg):
	if arg == "header":
		if isinstance(value, User):
			return value.username
		return value.title
	elif arg == "content":
		if isinstance(value, User):
			return value.about_me
		elif isinstance(value, Review):
			return value.body
		return value.summary
	elif arg == "type":
		if isinstance(value, User):
			return "用户"
		elif isinstance(value, Review):
			return "图解"
		return "电影"
	elif arg == 'href':
		if isinstance(value, User):
			return '/auser/homepage/'+str(value.id)
		elif isinstance(value,Review):
			return 	'/post/review_info/'+str(value.id)
		return '/movie_info/'+str(value.id)


